
package com.loan.rebate.config.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.loan.rebate.common.controller.BaseController;
import com.loan.rebate.config.param.ConfigInfoParam;
import com.loan.rebate.config.service.ConfigInfoService;
import com.loan.rebate.config.service.ConfigTypeService;
import com.loan.rebate.config.vo.ConfigInfoVO;
import com.loan.rebate.config.vo.ConfigTypeVO;

/**
 * 系统配置Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ConfigController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private ConfigInfoService configInfoService;

    @Autowired
    private ConfigTypeService configTypeService;

    private final static String CONFIG_TYPE_PAGE = "/config/config_main";

    private final static String CONFIG_TYPE_DETAIL_PAGE = "/config/config_type";

    private final static String CONFIG_INFO_DETAIL_PAGE = "/config/config_info";

    private final static String CONFIG_INFO_LIST_PAGE = "/config/config_info_list";

    @RequestMapping(value = "/check/config/type/update")
    @ResponseBody
    public String updateConfigType(ConfigTypeVO configType) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer insertCount = configTypeService.update(configType);
            if (insertCount != null && insertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改配置类型成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改配置类型失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改配置类型出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改配置类型出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/check/config/type/toinsert")
    public ModelAndView intoInsertConfigType() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ConfigTypeVO configTypeVO = new ConfigTypeVO();
        resultMap.put("configTypeVO", configTypeVO);
        return new ModelAndView(CONFIG_TYPE_DETAIL_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/config/type/list")
    public ModelAndView queryConfigTypeList() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ConfigTypeVO> dataList = configTypeService.queryConfigTypeList();
        resultMap.put("dataList", dataList);
        return new ModelAndView(CONFIG_TYPE_PAGE, resultMap);
    }

    @RequestMapping(value = "/check/config/info/list", produces = "text/html;charset=utf-8;")
    @ResponseBody
    public ModelAndView queryConfigInfoList(ConfigInfoParam param) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ConfigInfoVO> dataList = configInfoService.queryByPage(param);
        resultMap.put("paramMap", param);
        resultMap.put("dataList", dataList);
        return new ModelAndView(CONFIG_INFO_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/check/config/info/toinsert")
    public ModelAndView intoInsertConfigInfo(String configTypeId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ConfigInfoVO configInfo = new ConfigInfoVO();
        configInfo.setConfigTypeId(configTypeId);
        resultMap.put("configInfo", configInfo);
        return new ModelAndView(CONFIG_INFO_DETAIL_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/config/info/detail/{configId}")
    public ModelAndView viewConfigInfo(@PathVariable String configId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ConfigInfoVO configInfo = configInfoService.findById(configId);
        resultMap.put("configInfo", configInfo);
        return new ModelAndView(CONFIG_INFO_DETAIL_PAGE, resultMap);
    }

    @RequestMapping(value = "/check/config/info/update")
    @ResponseBody
    public String updateConfigInfo(ConfigInfoVO configInfo) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer updateCount = configInfoService.update(configInfo);
            if (updateCount != null && updateCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改配置项成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改配置项失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改配置项出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改配置项出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
}
